package com.eapi.project.utils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class IdentifyingCode {
    private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final String NUMBERS = "0123456789";

    public static String getCode() {
        // 初始化随机数生成器
        SecureRandom secureRandom = new SecureRandom();

        // 创建一个列表用于存放验证码的字符
        List<Character> codeChars = new ArrayList<>();

        // 添加四位随机字母
        for (int i = 0; i < 4; i++) {
            codeChars.add(ALPHABET.charAt(secureRandom.nextInt(ALPHABET.length())));
        }

        // 添加一位随机数字
        codeChars.add(NUMBERS.charAt(secureRandom.nextInt(NUMBERS.length())));

        // 随机打乱字符顺序，确保数字位置随机
        Collections.shuffle(codeChars, secureRandom);

        // 将字符列表转换为字符串
        StringBuilder sb = new StringBuilder();
        for (char c : codeChars) {
            sb.append(c);
        }
        return sb.toString();
    }
}
